{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Imports for pyOC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pyOC\n",
    "from pyOC import opencalphad as oc\n",
    "from pyOC import PhaseStatus as phStat\n",
    "from pyOC import GridMinimizerStatus as gmStat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Setting verbosity (True or False - default), if set to yes, in particular, when getters are called the returned values are displayed in a comprehensive way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "oc.setVerbosity(True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### reading database (.tdb file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): reading /home/rl211391/develop/OpenCalphad/examples/macros//steel7.TDB\n",
      "(OpenCalphad being verbose): component (6) names: ['C' 'CR' 'FE' 'MO' 'SI' 'V']\n"
     ]
    }
   ],
   "source": [
    "tdbFile=os.environ.get('OCPUBLICDATA')+'/steel7.TDB'\n",
    "oc.readtdb(tdbFile)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Play with phase status"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): modifying phases *  to status PhaseStatus.Suspended\n",
      "(OpenCalphad being verbose): modifying phases FCC_A1 M23C6 M6C to status PhaseStatus.Entered\n"
     ]
    }
   ],
   "source": [
    "oc.setPhasesStatus(('* ',),phStat.Suspended)\n",
    "phaseNames=('FCC_A1','M23C6','M6C')\n",
    "oc.setPhasesStatus(phaseNames,phStat.Entered)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set pressure and temperature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): setting pressure to 1.00e+05 Pa\n",
      "(OpenCalphad being verbose): setting temperature to 1173.00 K\n"
     ]
    }
   ],
   "source": [
    "oc.setPressure(1E5)\n",
    "oc.setTemperature(1173)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Set element molar amounts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): setting molar amount 0.0400 for element C (0)\n",
      "(OpenCalphad being verbose): setting molar amount 0.0600 for element CR (1)\n",
      "(OpenCalphad being verbose): setting molar amount 0.0500 for element MO (3)\n",
      "(OpenCalphad being verbose): setting molar amount 0.0030 for element SI (4)\n",
      "(OpenCalphad being verbose): setting molar amount 0.0100 for element V (5)\n",
      "(OpenCalphad being verbose): setting molar amount 0.8370 for element FE (2)\n"
     ]
    }
   ],
   "source": [
    "elementMolarAmounts = {\n",
    "\t'C' : 0.04,\n",
    "\t'CR' : 0.06,\n",
    "\t'MO': 0.05,\n",
    "\t'SI': 0.003,\n",
    "\t'V': 0.01,\n",
    "\t'FE': 1.0-0.04-0.06-0.05-0.003-0.01\n",
    "}\n",
    "oc.setElementMolarAmounts(elementMolarAmounts)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculate equilibrium without the grid-minimizer (equilibrium record is 'eq2')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): creating and selecting new equilibrium record 'eq2' ('EQ2')\n",
      "(OpenCalphad being verbose): calculating equilibrium with grid minimizer GridMinimizerStatus.Off\n"
     ]
    }
   ],
   "source": [
    "oc.changeEquilibriumRecord('eq2')\n",
    "oc.calculateEquilibrium(gmStat.Off)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Calculate equilibrium with the grid-minimizer (equilibrium record is 'default equilibrium')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): selecting equilibrium record 'default equilibrium' ('DEFAULT_EQUILIBRIUM')\n",
      "(OpenCalphad being verbose): calculating equilibrium with grid minimizer GridMinimizerStatus.On\n"
     ]
    }
   ],
   "source": [
    "oc.changeEquilibriumRecord()\n",
    "oc.calculateEquilibrium()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieving Gibbs energies and comparing them"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): selecting equilibrium record 'default equilibrium' ('DEFAULT_EQUILIBRIUM')\n",
      "(OpenCalphad being verbose): retrieving G: -5.767378e+04\n",
      "(OpenCalphad being verbose): selecting equilibrium record 'eq2' ('EQ2')\n",
      "(OpenCalphad being verbose): retrieving G: -5.760589e+04\n",
      "(OpenCalphad being verbose): selecting equilibrium record 'default equilibrium' ('DEFAULT_EQUILIBRIUM')\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "G=-5.767378e+04 (vs. without grid-minimizer: -5.760589e+04)\n"
     ]
    }
   ],
   "source": [
    "oc.changeEquilibriumRecord()\n",
    "G=oc.getGibbsEnergy() # a scalar\n",
    "oc.changeEquilibriumRecord('eq2')\n",
    "G2=oc.getGibbsEnergy() # a scalar\n",
    "print('G={0:e} (vs. without grid-minimizer: {1:e})'.format(G,G2))\n",
    "oc.changeEquilibriumRecord()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieving chemical potentials"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): retrieving MU:\n",
      "{\n",
      "    \"C\": -40277.342035801805,\n",
      "    \"CR\": -68893.39293504054,\n",
      "    \"FE\": -55314.718539072106,\n",
      "    \"MO\": -74567.11408055216,\n",
      "    \"SI\": -196024.33490496737,\n",
      "    \"V\": -131423.24634989392\n",
      "}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mu_FE=  -55314.718539072106\n"
     ]
    }
   ],
   "source": [
    "mu=oc.getChemicalPotentials() # a dictionary (keys are element names, values are chemical potentials)\n",
    "print('mu_FE= ',mu['FE'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieving equilibrium phases composition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): phases at equilibrium:\n",
      "phase molar amounts:\n",
      "{\n",
      "    \"FCC_A1#1\": 0.01920542894617253,\n",
      "    \"M23C6\": 0.02977621280681491,\n",
      "    \"M6C\": 0.09441638360236765,\n",
      "    \"FCC_A1_AUTO#2\": 0.856601974644645\n",
      "}\n",
      "phase element composition:\n",
      "{\n",
      "    \"FCC_A1#1\": {\n",
      "        \"C\": 0.45665297099480046,\n",
      "        \"CR\": 0.03426417107693683,\n",
      "        \"FE\": 0.0020841630748334116,\n",
      "        \"MO\": 0.1443298386826512,\n",
      "        \"SI\": 6.681137603878831e-10,\n",
      "        \"V\": 0.3626688555026644\n",
      "    },\n",
      "    \"M23C6\": {\n",
      "        \"C\": 0.2068965517241379,\n",
      "        \"CR\": 0.3414624222993837,\n",
      "        \"FE\": 0.3838209757158874,\n",
      "        \"MO\": 0.06769132200309422,\n",
      "        \"SI\": 0.0,\n",
      "        \"V\": 0.00012872825749663693\n",
      "    },\n",
      "    \"M6C\": {\n",
      "        \"C\": 0.14285714285714285,\n",
      "        \"CR\": 0.06508399462251023,\n",
      "        \"FE\": 0.38133540345297173,\n",
      "        \"MO\": 0.39191933121811307,\n",
      "        \"SI\": 0.0,\n",
      "        \"V\": 0.018804127849262088\n",
      "    },\n",
      "    \"FCC_A1_AUTO#2\": {\n",
      "        \"C\": 0.013519853563065565,\n",
      "        \"CR\": 0.050232768562391776,\n",
      "        \"FE\": 0.9216963669684142,\n",
      "        \"MO\": 0.009583081354863507,\n",
      "        \"SI\": 0.003502209983129125,\n",
      "        \"V\": 0.001465719568135902\n",
      "    }\n",
      "}\n",
      "phase sites:\n",
      "{\n",
      "    \"FCC_A1#1\": [\n",
      "        1.0,\n",
      "        1.0\n",
      "    ],\n",
      "    \"M23C6\": [\n",
      "        20.0,\n",
      "        3.0,\n",
      "        6.0\n",
      "    ],\n",
      "    \"M6C\": [\n",
      "        2.0,\n",
      "        2.0,\n",
      "        2.0,\n",
      "        1.0\n",
      "    ],\n",
      "    \"FCC_A1_AUTO#2\": [\n",
      "        1.0,\n",
      "        1.0\n",
      "    ]\n",
      "}\n",
      "phase constituent composition:\n",
      "{\n",
      "    \"FCC_A1#1\": {\n",
      "        \"sublattice 0\": {\n",
      "            \"CR\": 0.06306130198166399,\n",
      "            \"FE\": 0.0038357862720796564,\n",
      "            \"MO\": 0.26563104420926176,\n",
      "            \"SI\": 1.229626232816835e-09,\n",
      "            \"V\": 0.6674718663073684\n",
      "        },\n",
      "        \"sublattice 1\": {\n",
      "            \"C\": 0.8404444059091938,\n",
      "            \"VA\": 0.15955559409080622\n",
      "        }\n",
      "    },\n",
      "    \"M23C6\": {\n",
      "        \"sublattice 0\": {\n",
      "            \"CR\": 0.4730948138378144,\n",
      "            \"FE\": 0.5268934005119624,\n",
      "            \"V\": 1.1785650223144731e-05\n",
      "        },\n",
      "        \"sublattice 1\": {\n",
      "            \"CR\": 0.14683798997528025,\n",
      "            \"FE\": 0.19764676184049562,\n",
      "            \"MO\": 0.654349446029911,\n",
      "            \"V\": 0.0011658021543131923\n",
      "        },\n",
      "        \"sublattice 2\": {\n",
      "            \"C\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"M6C\": {\n",
      "        \"sublattice 0\": {\n",
      "            \"FE\": 1.0\n",
      "        },\n",
      "        \"sublattice 1\": {\n",
      "            \"MO\": 1.0\n",
      "        },\n",
      "        \"sublattice 2\": {\n",
      "            \"CR\": 0.2277939811787858,\n",
      "            \"FE\": 0.33467391208540104,\n",
      "            \"MO\": 0.37171765926339584,\n",
      "            \"V\": 0.06581444747241731\n",
      "        },\n",
      "        \"sublattice 3\": {\n",
      "            \"C\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"FCC_A1_AUTO#2\": {\n",
      "        \"sublattice 0\": {\n",
      "            \"CR\": 0.050921215945224446,\n",
      "            \"FE\": 0.9343283494325632,\n",
      "            \"MO\": 0.00971441887551069,\n",
      "            \"SI\": 0.0035502082791820494,\n",
      "            \"V\": 0.0014858074675196773\n",
      "        },\n",
      "        \"sublattice 1\": {\n",
      "            \"C\": 0.013705145118123152,\n",
      "            \"VA\": 0.9862948548818768\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "n_C in FCC_A1#1 =  0.45665297099480046\n",
      "a_i in FCC_A1#1 =  [1.0, 1.0]\n",
      "y_V^0 in liquid =  0.6674718663073684\n"
     ]
    }
   ],
   "source": [
    "phasesAtEquilibrium=oc.getPhasesAtEquilibrium() # a container class defined in pyOC.py\n",
    "phaseElementComposition=phasesAtEquilibrium.getPhaseElementComposition() # a dictionary (keys are phase names) of dictionaries (keys are the element names, values are molar fractions)\n",
    "print('n_C in FCC_A1#1 = ',phaseElementComposition['FCC_A1#1']['C'])\n",
    "phaseSites=phasesAtEquilibrium.getPhaseSites() # a dictionary (keys are phase names, values are arrays of number of sites whose sizes depend on the number of sublattices)\n",
    "print('a_i in FCC_A1#1 = ',phaseSites['FCC_A1#1'])\n",
    "phaseConstituentComposition=phasesAtEquilibrium.getPhaseConstituentComposition() # a dictionary (keys are phase names) of dictionaries per sublattice (keys are the constituent names, values are molar fractions)\n",
    "print('y_V^0 in liquid = ',phaseConstituentComposition['FCC_A1#1']['sublattice 0']['V'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Retrieving constituent composition (this info is 'updated' each time the getPhasesAtEquilibrium method is called by adding constituents that were not present before)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "(OpenCalphad being verbose): constituents description:\n",
      "{\n",
      "    \"CR\": {\n",
      "        \"mass\": 51.996,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {\n",
      "            \"CR\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"FE\": {\n",
      "        \"mass\": 55.846999999999994,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {\n",
      "            \"FE\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"MO\": {\n",
      "        \"mass\": 95.94,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {\n",
      "            \"MO\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"SI\": {\n",
      "        \"mass\": 28.085,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {\n",
      "            \"SI\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"V\": {\n",
      "        \"mass\": 50.941,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {\n",
      "            \"V\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"C\": {\n",
      "        \"mass\": 12.011000000000001,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {\n",
      "            \"C\": 1.0\n",
      "        }\n",
      "    },\n",
      "    \"VA\": {\n",
      "        \"mass\": 0.0,\n",
      "        \"charge\": 0.0,\n",
      "        \"elements\": {}\n",
      "    }\n",
      "}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m_V =  50.941\n",
      "q_V =  0.0\n",
      "stoi^V_V =  1.0\n"
     ]
    }
   ],
   "source": [
    "constituentsDescription = oc.getConstituentsDescription() # a dictionary (keys are the constituent names) of dictionaries (keys are 'mass', 'charge' and 'elements', values are respectively the constituent molar mass, the constituent eletrical charge and a dictionary - keys are element names, values are stoichiometric coefficients)\n",
    "print('m_V = ',constituentsDescription['V']['mass'])\n",
    "print('q_V = ',constituentsDescription['V']['charge'])\n",
    "print('stoi^V_V = ',constituentsDescription['V']['elements']['V'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
